<?php
/**
 * Subclass for performing query and update operations on the 'usuario' table.
 *
 *
 *
 * @package lib.model
 */
class UsuarioPeer extends BaseUsuarioPeer
{
  static function getByCredencial($credencial) {
    $c = new Criteria();
    $c->addJoin(self::ROL_ID,RolPeer::ID);
    $c->addJoin(RolPeer::ID,RolCredencialPeer::ROL_ID);
    $c->addJoin(RolCredencialPeer::CREDENCIAL_ID,CredencialPeer::ID);
    $c->add(CredencialPeer::CODIGO,$credencial);
    return self::doSelect($c);
  }
  static public function getLogin($username,$password){
    $c = new Criteria();
    $c->add(self::USERNAME,$username);
    $c->add(self::PASSWORD,$password);
    return self::doSelectOne($c);
  }
  static public function getMedicosByEstudioId($estudio_id){
    $c = new Criteria();
    $c->addJoin(self::ID,UsuarioEstudioPeer::USUARIO_ID);
    $c->add(UsuarioEstudioPeer::ESTUDIO_ID,$estudio_id);
    $c->add(UsuarioEstudioPeer::ACTIVO,1);
    return self::doSelect($c);
  }
  static public function getSqlByFechaHoraEstudio($dia,$hora,$dia_id,$estudio_id){
    $hora = explode(":",$hora);
    $minutos = $hora[1];
    $hora = $hora[0];
    $sql = "
     select u.id
     from usuario as u
     join horario as h on u.id = h.usuario_id where  h.dia_id = $dia_id and (h.inicio <= $hora and $hora <= fin)
     and (select count(*) from horario_cambio where usuario_id = u.id and (fecha_inicio <= '$dia' and '$dia' <= fecha_fin and activo = 1)= 0)
     and (select count(*) from usuario_estudio where usuario_id = u.id and estudio_id = $estudio_id and activo = 1) != 0
     ";
    return $sql;
  }
}
